package com.ruoyi.web.controller.construction;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.ConstructionVideo;
import com.ruoyi.system.service.construction.ConstructionVideoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 施工队视频信息Controller
 * 
 * @author 张少丛
 * @date 2020-11-07
 */
@RestController
@RequestMapping("/construction/video")
public class ConstructionVideoController extends BaseController
{
    @Autowired
    private ConstructionVideoService constructionVideoService;

    /**
     * 查询施工队视频信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConstructionVideo constructionVideo)
    {
        startPage();
        List<ConstructionVideo> list = constructionVideoService.selectConstructionVideoList(constructionVideo);
        return getDataTable(list);
    }

    /**
     * 导出施工队视频信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:video:export')")
    @Log(title = "施工队视频信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ConstructionVideo constructionVideo)
    {
        List<ConstructionVideo> list = constructionVideoService.selectConstructionVideoList(constructionVideo);
        ExcelUtil<ConstructionVideo> util = new ExcelUtil<ConstructionVideo>(ConstructionVideo.class);
        return util.exportExcel(list, "video");
    }

    /**
     * 获取施工队视频信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('construction:video:query')")
    @GetMapping(value = "/{constructionVideoId}")
    public AjaxResult getInfo(@PathVariable("constructionVideoId") Integer constructionVideoId)
    {
        return AjaxResult.success(constructionVideoService.selectConstructionVideoById(constructionVideoId));
    }

    /**
     * 新增施工队视频信息
     */
    @PreAuthorize("@ss.hasPermi('construction:video:add')")
    @Log(title = "施工队视频信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConstructionVideo constructionVideo)
    {
        return toAjax(constructionVideoService.insertConstructionVideo(constructionVideo));
    }

    /**
     * 修改施工队视频信息
     */
    @PreAuthorize("@ss.hasPermi('construction:video:edit')")
    @Log(title = "施工队视频信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstructionVideo constructionVideo)
    {
        return toAjax(constructionVideoService.updateConstructionVideo(constructionVideo));
    }

    /**
     * 删除施工队视频信息
     */
    @PreAuthorize("@ss.hasPermi('construction:video:remove')")
    @Log(title = "施工队视频信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{constructionVideoIds}")
    public AjaxResult remove(@PathVariable Integer[] constructionVideoIds)
    {
        return toAjax(constructionVideoService.deleteConstructionVideoByIds(constructionVideoIds));
    }

    /**
     * 上传视频
     * @param file
     * @return
     * @throws IOException
     */
    @Log(title = "施工队视频信息", businessType = BusinessType.INSERT)
    @PostMapping("/uploadVideo")
    public AjaxResult uploadVideo(@RequestParam("videoFile") MultipartFile file,@RequestParam("constructionId") Integer constructionId) throws IOException {
        System.out.println("constructionId:"+constructionId);
        if (!file.isEmpty())
        {
            String videoPath = RuoYiConfig.getVideoPath();
            String video = FileUploadUtils.uploadVideo(videoPath, file);
            ConstructionVideo constructionVideo = new ConstructionVideo();
            constructionVideo.setConstructionInfoId(constructionId.intValue());
            constructionVideo.setVidUrl("http://39.98.237.139"+video);
            return toAjax(constructionVideoService.insertConstructionVideo(constructionVideo));
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
